package net.apps123.commonaclasses;

import java.util.ArrayList;
import java.util.List;

import net.apps123.interfaces.ScalingListener;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * X轴视图
 */
public class Xaxis extends View {

    // 柱状图还是线图
    public enum XaxisType {
        Cpb,
        Lin
    }

    private List<ScalingListener> listeners = new ArrayList<ScalingListener>();

    public int nTics = 4;
    XaxisType type;
    double startFreq;
    double endFreq;
    Rect rect;
    float height;
    float width;
    int size;
    Paint penBlack;

    //坐标值
    String[] cpbFreqs = new String[]{"31.5", "63", "125", "250", "500", "1k", "2k", "4k", "8k", "16K"};
    String[] linFreqs = new String[]{"0k", "2k", "4k", "6k",  "8k", "10K",
            "12k", "14k", "16k", "18k", "20K", "22K", ""};

    public Xaxis(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public Xaxis(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public Xaxis(Context context) {
        super(context);
    }

    //设置缩放
    public void addListener(ScalingListener listener) {
        listeners.add(listener);
    }

    //更新缩放
    void notifyNewScaling(int base, int range) {
        for (ScalingListener listener : listeners) {
            listener.ScalingHappened(base, range);
        }
    }

    //初始化X轴

    /**
     * 初始化X轴
     * @param type
     * @param startFreq
     * @param endFreq
     */
    public void Init(XaxisType type, double startFreq, double endFreq) {
        this.type = type;
        this.startFreq = startFreq;
        this.endFreq = endFreq;
        size = getResources().getDimensionPixelSize(R.dimen.myFontSize);
        penBlack = new Paint();
        penBlack.setColor(Color.DKGRAY);
        penBlack.setStyle(Paint.Style.FILL);
        penBlack.setStrokeWidth(1);
        penBlack.setTextSize(size);
        rect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (rect == null)
            return;

        super.onDraw(canvas);
        getDrawingRect(rect);
        height = rect.height();
        width = rect.width();

        if (type == XaxisType.Cpb) {
            for (int i = 0; i < cpbFreqs.length; i++) {
                float x = width / 54 * 3 + width / 10 * i;
                canvas.drawLine(x, height / 3, x, 0, penBlack);
                canvas.drawText(cpbFreqs[i], x, height, penBlack);
            }
        } else if (type == XaxisType.Lin) {
            for (int i = 0; i < linFreqs.length; i++) {
                float x = width / (linFreqs.length - 1) * i;
                if (i != linFreqs.length - 1) {
                    canvas.drawLine(x, height / 3, x, 0, penBlack);
                    canvas.drawText(linFreqs[i], x, height, penBlack);
                } else {
                    canvas.drawLine(x, height / 3, x - 1, 0, penBlack);
                    canvas.drawText(linFreqs[i], x - 2 * size, height, penBlack);
                }
            }

        }
    }
}
